package com.link2room.optimus.facade.impl;

import com.link2room.optimus.dto.param.manager.PricePoolManagerParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.optimus.entity.PricePool;
import com.link2room.optimus.dto.result.base.PricePoolDto;
import com.link2room.optimus.facade.IPricePoolFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by sss on 2016/8/31.
 */
@Component("pricePoolFacadeService")
public class PricePoolFacadeServiceImpl implements IPricePoolFacadeService {
    @Resource
    private ICommonService commonService;

    /**
     * 保存价格池
     * @param pricePoolManagerParamDto
     * @throws Exception
     */
    @Override
    public void savePricePool(PricePoolManagerParamDto pricePoolManagerParamDto)throws Exception{
        PricePool pricePool = ClassConverUtil.copyProperties(pricePoolManagerParamDto.getPricePool(), PricePool.class);
        commonService.save(pricePool);
    }

    /**
     * 更新价格池
     * @param pricePoolManagerParamDto
     * @throws Exception
     */
    @Override
    public void updatePricePool(PricePoolManagerParamDto pricePoolManagerParamDto)throws Exception{
        PricePool pricePool= commonService.findOneById(PricePool.class, pricePoolManagerParamDto.getPricePool().getId());
        if(pricePool!=null){
            ClassConverUtil.copyProperties(pricePoolManagerParamDto.getPricePool(),pricePool);
            commonService.merge(pricePool);
        }
    }

    /**
     * 列出价格池列表
     * @param pricePoolManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<PricePoolDto> listPricePools(PricePoolManagerParamDto pricePoolManagerParamDto)throws Exception{
        QueryResult<PricePool> pricePoolResult = commonService.findAllEQ(PricePool.class,
                ClassConverUtil.copyProperties(pricePoolManagerParamDto,PricePool.class),
                pricePoolManagerParamDto.getCurrentPage(),
                pricePoolManagerParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(pricePoolResult, PricePoolDto.class, false);
    }

    /**
     * 查询单个价格池
     * @param pricePoolManagerParamDto
     * @return
     */
    @Override
    public PricePoolDto getPricePool(PricePoolManagerParamDto pricePoolManagerParamDto) throws Exception {
        PricePool pricePool = commonService.findOneById(PricePool.class, pricePoolManagerParamDto.getId());
        return ClassConverUtil.copyPropertiesToDto(pricePool,PricePoolDto.class, false);
    }
}
